{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "63396492-2899-4862-8cfb-84e812a844cc",
   "metadata": {},
   "source": [
    "# Parameter `base` of the Waterfall Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b1b75d26-0fde-4591-af27-677b5d807b5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"laOyJC\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.5.1/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"laOyJC\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"laOyJC\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7137dd20-92ce-4b54-87d1-a1088285f1d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "val dataMap = mapOf(\n",
    "    \"Accounts\" to listOf(\"Product revenue\", \"Services revenue\", \"Fixed costs\", \"Variable costs\"),\n",
    "    \"Values\" to listOf(830_000, 290_000, -360_000, -150_000),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f861d4a-b5df-429e-b4e0-a2a925c4cb28",
   "metadata": {},
   "source": [
    "## Default View"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "201f8210-dfc1-4df2-b1eb-3e6c5cc1a92b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"iWHWNS\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "   \n",
       "   (function() {\n",
       "   // ----------\n",
       "   \n",
       "   var containerDiv = document.getElementById(\"iWHWNS\");\n",
       "   var observer = new ResizeObserver(function(entries) {\n",
       "       for (let entry of entries) {\n",
       "           var width = containerDiv.clientWidth\n",
       "           if (entry.contentBoxSize && width > 0) {\n",
       "           \n",
       "               // Render plot\n",
       "               if (observer) {\n",
       "                   observer.disconnect();\n",
       "                   observer = null;\n",
       "               }\n",
       "\n",
       "               var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"labels\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Total\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,1120000.0,760000.0,610000.0,610000.0],\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..value..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..dy..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[830000.0,1120000.0,1120000.0,760000.0,null],\n",
       "\"..ymin..\":[0.0,830000.0,760000.0,610000.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,1120000.0,760000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[null,null,null,null,610000.0],\n",
       "\"..ymin..\":[null,null,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymiddle..\":[415000.0,975000.0,940000.0,685000.0,305000.0],\n",
       "\"..label..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..ymiddle..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "},{\n",
       "\"geom\":\"hline\",\n",
       "\"yintercept\":0.0,\n",
       "\"linetype\":2,\n",
       "\"tooltips\":\"none\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":{\n",
       "\"blank\":true\n",
       "},\n",
       "\"name\":\"minimal\"\n",
       "},\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"spec_id\":\"1\"\n",
       "};\n",
       "               window.letsPlotCall(function() {\n",
       "       \n",
       "               var toolbar = null;\n",
       "               var plotContainer = containerDiv;               \n",
       "               \n",
       "                   var options = {\n",
       "                       sizing: {\n",
       "                           width_mode: \"min\",\n",
       "                           height_mode: \"scaled\",\n",
       "                           width: width\n",
       "                       }\n",
       "                   };\n",
       "                   var fig = LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer, options);\n",
       "                   if (toolbar) {\n",
       "                     toolbar.bind(fig);\n",
       "                   }\n",
       "               });\n",
       "               \n",
       "               break;\n",
       "           }\n",
       "       }\n",
       "   });\n",
       "   \n",
       "   observer.observe(containerDiv);\n",
       "   \n",
       "   // ----------\n",
       "   })();\n",
       "   \n",
       "   </script>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "waterfallPlot(dataMap, \"Accounts\", \"Values\", hline = elementLine()) + themeMinimal()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f428414-25a3-49ec-b358-b04c89c80586",
   "metadata": {},
   "source": [
    "## Change Base"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "30a03d80-e4c6-4e42-8467-a4f3e1e33778",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"r2P4He\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "   \n",
       "   (function() {\n",
       "   // ----------\n",
       "   \n",
       "   var containerDiv = document.getElementById(\"r2P4He\");\n",
       "   var observer = new ResizeObserver(function(entries) {\n",
       "       for (let entry of entries) {\n",
       "           var width = containerDiv.clientWidth\n",
       "           if (entry.contentBoxSize && width > 0) {\n",
       "           \n",
       "               // Render plot\n",
       "               if (observer) {\n",
       "                   observer.disconnect();\n",
       "                   observer = null;\n",
       "               }\n",
       "\n",
       "               var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"labels\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Total\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..value..\":[1230000.0,1520000.0,1160000.0,1010000.0,1010000.0],\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..value..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..dy..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[1230000.0,1520000.0,1520000.0,1160000.0,null],\n",
       "\"..ymin..\":[400000.0,1230000.0,1160000.0,1010000.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[1230000.0,1520000.0,1160000.0,1010000.0,1010000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[null,null,null,null,1010000.0],\n",
       "\"..ymin..\":[null,null,null,null,400000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymiddle..\":[815000.0,1375000.0,1340000.0,1085000.0,705000.0],\n",
       "\"..label..\":[830000.0,290000.0,-360000.0,-150000.0,1010000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..ymiddle..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "},{\n",
       "\"geom\":\"hline\",\n",
       "\"yintercept\":400000.0,\n",
       "\"linetype\":2,\n",
       "\"tooltips\":\"none\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":{\n",
       "\"blank\":true\n",
       "},\n",
       "\"name\":\"minimal\"\n",
       "},\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"spec_id\":\"2\"\n",
       "};\n",
       "               window.letsPlotCall(function() {\n",
       "       \n",
       "               var toolbar = null;\n",
       "               var plotContainer = containerDiv;               \n",
       "               \n",
       "                   var options = {\n",
       "                       sizing: {\n",
       "                           width_mode: \"min\",\n",
       "                           height_mode: \"scaled\",\n",
       "                           width: width\n",
       "                       }\n",
       "                   };\n",
       "                   var fig = LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer, options);\n",
       "                   if (toolbar) {\n",
       "                     toolbar.bind(fig);\n",
       "                   }\n",
       "               });\n",
       "               \n",
       "               break;\n",
       "           }\n",
       "       }\n",
       "   });\n",
       "   \n",
       "   observer.observe(containerDiv);\n",
       "   \n",
       "   // ----------\n",
       "   })();\n",
       "   \n",
       "   </script>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val prevPeriodTotal = 400_000\n",
    "waterfallPlot(dataMap, \"Accounts\", \"Values\", base = prevPeriodTotal, hline = elementLine()) + themeMinimal()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
